package com.sbm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @version 创建时间：2014-3-10 下午3:17:26
 * @declare
 */

public class DateUtil {

    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd hh:mm:ss
     * @return 已格式化的时间
     */
    public static String getNotifyDate(Date date) {
        return new SimpleDateFormat("yyyy年MM月dd日 HH时mm分",Locale.getDefault()).format(date) ;
//		return new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.getDefault()).format(date) ;
    }

    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd hh:mm:ss
     * @return 已格式化的时间
     */
    public static String getLongDate() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault()).format(new Date()) ;
    }

    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd hh:mm:ss
     * @return 已格式化的时间
     */
    public static String getLongDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault()).format(date) ;
    }

    /**
     * 获取指定时间的标准起始时间,格式：yyyy-MM-dd 00:00:00
     *
     * @param date	需要格式化的时间对象
     * @return 已格式化的时间
     */
    public static Date getBegin(Date date) {
        Calendar cal = Calendar.getInstance();
        if(date == null){
            cal.setTime(new Date());
        }else{
            cal.setTime(date);
        }
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取指定时间的标准结束时间,格式:yyyy-MM-dd 23:59:59
     *
     * @return 已格式化的时间
     */
    public static Date getEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        if(date == null){
            cal.setTime(new Date());
        }else{
            cal.setTime(date);
        }
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd hh:mm:ss
     *
     * @param date 需要格式化的时间对象
     *
     * @return 已格式化的时间
     */
    public static String getChineseTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }


    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd
     * @param date 需要格式化的时间对象
     * @return 已格式化的时间
     */
    public static String getShortDate() {
        return new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()).format(new Date()) ;
    }

    /**
     * 获取标准的中国时间,格式：yyyy-MM-dd
     * @param date 需要格式化的时间对象
     * @return 已格式化的时间
     */
    public static String getShortDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()).format(date) ;
    }

    /**
     * 将长日期格式字符串转换为日期  yyyy-MM-dd HH:mm:ss
     * @param dateString
     * @return
     */
    public static Date parseLongDate(String dateString) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault()).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将短格式自动补齐为 23:59:59
     * @param dateString
     * @return
     */
    public static Date parseShortDateEndDay(String dateString) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.getDefault()).parse(dateString + " 23:59:59" );
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将长日期格式字符串转换为日期  yyyy-MM-dd
     * @param dateString
     * @return
     */
    public static Date parseShortDate(String dateString) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parseDateFromString(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseDateFromStringSlash(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseYMDHSDateFromString(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseChineseDateFromString(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseHms(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(format.parse(dateString));

            Calendar now = Calendar.getInstance();
            cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
            cal.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR));

            date = cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseYM(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = null;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(format.parse(dateString));

            Calendar now = Calendar.getInstance();
            /*cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, now.get(Calendar.MONTH));*/
            cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

            date = cal.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseYMDFromString(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    public static Date parseyyyyMMddHHmmss(String dateString) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 返回 时间格式：20151120181000
     * @param date
     * @return
     */
    public static String getyyyyMMddHHmmss(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    public static String getSimpleChineseTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return date == null ? "" : format.format(date);
    }

    public static String getYMD(Long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return date == null ? "" : format.format(date);
    }

    public static String getYM(Long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        return date == null ? "" : format.format(date);
    }

    public static String getYYYYMMDD(Long timeMillis) {
        Date date = new Date(timeMillis);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return date == null ? "" : format.format(date);
    }
    public static String getYMDTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getYMDHMTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static String getHms(Date d) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return d == null ? "" : format.format(d);
    }

    public static String getHm(Date d) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return d == null ? "" : format.format(d);
    }

    /**
     * 将时间转换成中文形式
     */
    public static String DateToCN(Date date) {
        if (null == date || "".equals(date)) {
            return null;
        }
        String[] array = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十",
                "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一",
                "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一",
                "三十三", "三十三", "三十四", "三十五", "三十六", "三十七", "三十八", "三十九", "四十", "四十一",
                "四十四", "四十四", "四十四", "四十五", "四十六", "四十七", "四十八", "四十九", "五十", "五十一",
                "五十五", "五十五", "五十四", "五十五", "五十六", "五十七", "五十八", "五十九" };

        StringBuffer cn = new StringBuffer();

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cn.append(array[cal.get(Calendar.HOUR_OF_DAY)]);
        cn.append("时");
        cn.append(array[cal.get(Calendar.MINUTE)]);
        cn.append("分");

        return cn.toString();

    }

    /**
     * 返回整数的年龄
     * @param birthday
     * @param nowdate
     * @return
     */
    public static int getAge(Date birthday,Date nowdate) {
        if (birthday.getTime() > nowdate.getTime()) {
            return 0 ;
        }

        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(birthday);
        aft.setTime(nowdate);

        int imonths = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int iyears = aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR);
        int result1 = iyears * 12 + imonths;
        if (result1 == 0) {
            return 1;
        } else {
            return result1 / 12;
        }

    }

    /**
     * 检查是否日期
     * @param dateString
     * @return
     */
    public static boolean isDate(String dateString) {
        try {
            @SuppressWarnings("unused")
            Date date = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault()).parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 从有效时间计算失效时间
     * @param date  当前时间
     * @param time_expire  有效时间（分钟）
     * @return
     */
    public static Date getNextDate(Date date, int time_expire_minute) {
        Calendar cal = Calendar.getInstance();
        if(date == null){
            cal.setTime(new Date());
        }else{
            cal.setTime(date);
        }
        cal.add(Calendar.MINUTE, time_expire_minute);
        return cal.getTime();
    }

    public static String getAge(Date birthday) {
        Date now = new Date();
        if (birthday.getTime() > now.getTime()) {
            return "错误";
        }

        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(birthday);
        aft.setTime(now);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);

        int year = aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR);

        int result1 = year * 12 + result;

        if (result1 == 0) {
            result1 = 1;
        }

        if (result1 < 12) {
            return result1 + "月";
        } else {
            return (result1 / 12) + "岁";
        }

    }

}
